<?php 
ob_start();
session_start();
include "inc/conn.php";
$tts = date("YmdHis",time());
$password = "ffep";
$user_name = "andy";
if(isset($_POST['submit5'])){
    if($_POST['add_pwd']==$password){
    	$_SESSION['add_pwd']=$_POST['add_pwd'];
    	$_SESSION['user_name']=$_POST['user_name'];
    	// echo "<script>window.location='scan.php'</script>";
      header("location:add.php?v={$tts}");
    } else{echo "<script>alert('密码错误');</script>";}
}
 ?>
 <!DOCTYPE html>
 <html lang="zh-CN">
 <head>
 	<meta charset="UTF-8">
 	<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
 	<link href="https://lib.baomitu.com/twitter-bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet">
 	<link rel="stylesheet" href="http://at.alicdn.com/t/font_374778_ufdzobu4bkh.css">
 	<link href="inc/css/wap.css?v=<?php echo $tts;?>" rel="stylesheet" type="text/css" />
 	<title>导入数据</title>
 	<style>
 		.myform label{color: #ffffff;}
 	</style>
 	<style>
		.bgcard{background-image: linear-gradient(#009688, #0d1338 100%);}
		.top-ico .iconfont {font-size: 94px; color: #09BB07; }
		.top-ico {text-align: center; }
		.msg-box-txt {text-align: center; }
		.top-ico .iconfont.icon-warn {color: red; }
		.manage {position: absolute; right: 6px; top: 1px; }
		.manage .iconfont {color: #ffffff; font-size: 23px; }
		.sub_top{display: none;}
		.span_cnzz {position: fixed; bottom: 0; left: 0; visibility: hidden; }
		.foot, .foot a{color: #FF9800;}
		.qd_desc {margin-bottom: 15px; color: #ffffff; text-align: center; font-size: 14px; color: #fff497; }
		.page-content-msg {
		background-color: #ffffff;
		margin: 50px auto 0px;
		}
		.del-btn-box {display: flex; justify-content: space-between; }
		.del-btn-box >button {margin: 0; width: 48%; }
		.btn-block+.btn-block {margin: 0; }

 	</style>
 </head>
 <body>
 <div class="bgcard"></div>
<?php 
if($_GET['c'] == 'exit'){
	session_destroy();
	echo '
		<script>window.location.href="add.php?v='.$tts.'"</script>
	';
}
if(isset($_SESSION['add_pwd']) && $_SESSION['add_pwd'] == $password){
 if(isset($_POST['id']) && $_POST['id']==1){
 	$content = $_POST['txtadd'];
 	
 	//必须含有换行符号和TAB符
 	if($content == '' || !strpos($content, Chr(13)) || !strpos($content, Chr(9))){
 		echo "<div class=\"main\">\r\n";
 		echomsg("导入失败，数据格式不正确!",0);
 		echo '
 		<div class="so_boxesd">
 		  <input type="button" value="返 回" class="btn btn-primary btn-block" onclick="location.href=\'?t=back\';" id="reset">
 		</div>
 		</div>
 		';

 		exit;
 	}
$handle=fopen($file_qd_all,"a+");
//读取服务器端的session_id，如果没有的话赋值为-1
$session_id = empty($_SESSION['session_id']) ? -1 : $_SESSION['session_id'];

//读取客户端的post_id,如果没有的话赋值为-2
$post_id = empty($_POST['post_id']) ? -2 : $_POST['post_id'];

//判断两个id是否相同，相同则说明已经处理过一次，不同则进行处理
if($session_id == $post_id){
	// echo "<h1>已经处理过了，不管了</h1>";
} else{
	if($post_id == -2){

	} else{
		$str=fwrite($handle, $content);
		$arr_line = explode("\n",$content);
		$arr_cc = count($arr_line) - 1;
		$_SESSION['session_id'] = $post_id;
		echo "<div class=\"main\">\r\n";
		echomsg("导入成功",1,"成功导入{$arr_cc}条数据");
		echo '
		<div class="so_boxesd">
		  <input type="button" value="返 回" class="btn btn-primary btn-block" onclick="location.href=\'?t=back\';" id="reset">
		</div>
		</div>
		';
		exit;
	}
}
fclose($handle);
 	// echo $content;
 	// var_dump($content);
} ?>

<?php include 'menu.php'; ?>
<div class="main">
	<div class="qd_text_box">
	  <div class="qd_title"><h4>导入数据</h4></div>
	</div>
	<form name="myform" class="myform" method="post" action="add.php?v=<?php echo $tts; ?>">
		<input type="hidden" name="id" value="1">
		<input type="text" hidden="hidden" name="post_id" id="post_id" value="<?php echo $tts; ?>" /> 
		<div class="form-group">
			<label>直接从excel复制进来即可</label>
			<textarea id="txtadd" name="txtadd" class="form-control" rows="9" onfocus="this.placeholder=''" onblur="this.placeholder='请直接从excel复制进来，只需复制姓名和电话'" placeholder="请直接从excel复制进来，只需复制姓名和电话"></textarea>
		</div>
		<div class="form-group">
			<input class="btn btn-primary btn-block" type="submit" name="submit" value="提交">
		</div>
		
		<div class="form-group" >
			<a class="btn btn-outline-danger btn-block" href="add.php?c=exit">退出</a>
		</div>

	</form>
	<?php 
		if($_SESSION['user_name'] == $user_name){ ?>
		<script>
			function del_all(ss){
				if(confirm("你确定要删除所有数据吗，注意此操作不可恢复，请谨慎操作！")){
					window.location.href="add.php?del="+ss;
				}
			}
			// $(document).on('click','#del_all',del_all);
		</script>
		<div class="form-group del-btn-box" >
			<button id="del_all" type="button" onclick="del_all('all')" class="btn btn-danger btn-block del-all">清除报名数据</button>
			<button id="del_all_ok" type="button" onclick="del_all('all_ok')" class="btn btn-danger btn-block del-all-ok">清除签到数据</button>
		</div>
	
	<?php } ?>
</div>
<?php
if(isset($_GET['del'])){
	if($_GET['del'] == 'all'){
		$txt = '';
		if(file_put_contents($file_qd_all, $txt) !== FALSE) {
		}
		echo "<div style=\"text-align: center;color: #FFEB3B;position: fixed;top: 25px;width: 100%;left: 0;\">报名数据清除成功！</div>\r\n";
		echo '
		<script>
			setTimeout(function(){
				window.location.href="add.php?v='.$tts.'"
			},500)
			
		</script>
		';
	} elseif(($_GET['del'] == 'all_ok')){
		$txt = '';
		if(file_put_contents($file_qd_ok, $txt) !== FALSE) {
		}
		if(file_put_contents($file_qd_time, $txt) !== FALSE) {
		}
		echo "<div style=\"text-align: center;color: #FFEB3B;position: fixed;top: 25px;width: 100%;left: 0;\">签到数据清除成功！</div>\r\n";
		echo '
		<script>
			setTimeout(function(){
				window.location.href="add.php?v='.$tts.'"
			},500)
			
		</script>
		';

	} elseif($_GET['del'] != 'all' || $_GET['del'] != 'all_ok' ) {
		webalert("参数错误");
	} else{}
}

} else { 
	if(isset($_GET['del'])){
		if($_GET['del'] == 'all' || $_GET['del'] != 'all_ok'){
			echo "<div class=\"main\">\r\n";
			echomsg("你无权操作",0,"你无权操作，请联系管理员");
			echo '
			<div class="so_boxesd">
			  <input type="button" value="返 回" class="btn btn-primary btn-block" onclick="location.href=\'?t=back\';" id="reset">
			</div>
			</div>
			';
			exit;
			
		} elseif($_GET['del'] != 'all') {
			webalert("参数错误");
		}
	}

?>
<div class="main">
	<div class="qd_text_box">
	  <div class="qd_title"><h4>登录</h4></div>
	</div>
	<form name="form2" id="myform" class="myform" method="post" action="">
	    <input type="hidden" name="login" value="1">
	    <div class="form-group">
	    	<input class="form-control" name="user_name" type="text" id="user_name" placeholder="请输入用户名" value="admin">
	    </div>
	    <div class="form-group">
	    	<input class="form-control" name="add_pwd" type="password" id="add_pwd" placeholder="请输入密码">
	    </div>
	    <div class="form-group">
	    	<input class="btn btn-primary btn-block" type="submit" name="submit5" value="确定">
	    </div>
	</form>
</div>
<?php }
?>
<script src="https://lib.baomitu.com/jquery/2.1.4/jquery.min.js"></script>
<script src="inc/js/mobile-menu.js?v=<?php echo $tts;?>"></script>
<script>
$("textarea").on('keydown',function(e){
	if(e.keyCode == 9){
		e.preventDefault();
		var indent = '	';
		var start = this.selectionStart;
		var end = this.selectionEnd;
		var selected = window.getSelection().toString();
		selected = indent + selected.replace(/\n/g,'\n'+indent);
		this.value = this.value.substring(0,start) + selected + this.value.substring(end);
		this.setSelectionRange(start+indent.length,start+selected.length);
	}
})
</script>
 </body>
 </html>